﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace c_Formatting
{
    internal class Filetest
    {
        //public partial class Form1 : Form
        //{
        //    public Form1()
        //    {
        //        InitializeComponent();
        //    }
        //    private void btnChooseFile_Click(object sender, EventArgs e)
        //    {
        //        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        //        {
        //            string filePath = openFileDialog1.FileName;
        //            ProcessFile(filePath);
        //        }
        //    }
        //    private void ProcessFile(string filePath)
        //    {
        //        // 读取源文件的所有行
        //        string[] originalLines = File.ReadAllLines(filePath);

        //        // 原始行数统计
        //        int originalLineCount = originalLines.Length;

        //        // 原始单词数统计
        //        int originalWordCount = CountWords(originalLines);

        //        // 显示原始行数和单词数
        //        lblOriginalLines.Text = $"Original Lines: {originalLineCount}";
        //        lblOriginalWords.Text = $"Original Words: {originalWordCount}";

        //        // 删除空行和注释后的行
        //        string[] formattedLines = RemoveCommentsAndEmptyLines(originalLines);

        //        // 格式化后统计行数和单词数
        //        int formattedLineCount = formattedLines.Length;
        //        int formattedWordCount = CountWords(formattedLines);

        //        // 显示格式化后的行数和单词数
        //        lblFormattedLines.Text = $"Formatted Lines: {formattedLineCount}";
        //        lblFormattedWords.Text = $"Formatted Words: {formattedWordCount}";

        //        // 统计单词频率
        //        Dictionary<string, int> wordFrequency = CountWordFrequency(formattedLines);

        //        // 显示单词频率
        //        DisplayWordFrequency(wordFrequency);
        //    }
        //    private int CountWords(string[] lines)
        //    {
        //        int wordCount = 0;
        //        foreach (var line in lines)
        //        {
        //            // 使用正则表达式分隔单词，去掉标点符号
        //            string[] words = Regex.Split(line, @"\W+").Where(w => !string.IsNullOrEmpty(w)).ToArray();
        //            wordCount += words.Length;
        //        }
        //        return wordCount;
        //    }
        //    private Dictionary<string, int> CountWordFrequency(string[] lines)
        //    {
        //        Dictionary<string, int> wordFrequency = new Dictionary<string, int>();
        //        foreach (var line in lines)
        //        {
        //            string[] words = Regex.Split(line, @"\W+").Where(w => !string.IsNullOrEmpty(w)).ToArray();
        //            foreach (var word in words)
        //            {
        //                if (wordFrequency.ContainsKey(word))
        //                {
        //                    wordFrequency[word]++;
        //                }
        //                else
        //                {
        //                    wordFrequency[word] = 1;
        //                }
        //            }
        //        }
        //        return wordFrequency;
        //    }

        //    private string[] RemoveCommentsAndEmptyLines(string[] lines)
        //    {
        //        List<string> formattedLines = new List<string>();
        //        foreach (var line in lines)
        //        {
        //            // 去掉前后空白字符
        //            string trimmedLine = line.Trim();

        //            // 排除空行和以"//"开头的注释行
        //            if (!string.IsNullOrEmpty(trimmedLine) && !trimmedLine.StartsWith("//"))
        //            {
        //                formattedLines.Add(trimmedLine);
        //            }
        //        }
        //        return formattedLines.ToArray();
        //    }
        //    private void DisplayWordFrequency(Dictionary<string, int> wordFrequency)
        //    {
        //        listViewWords.Items.Clear();
        //        foreach (var kvp in wordFrequency)
        //        {
        //            ListViewItem item = new ListViewItem(kvp.Key);
        //            item.SubItems.Add(kvp.Value.ToString());
        //            listViewWords.Items.Add(item);
        //        }
        //    }
        //}
        public class Lefttime : EventArgs
        {
            public int LeftTime { get; private set; }  // 剩余时间
            public Lefttime(int leftTime)
            {
                LeftTime = leftTime;
            }
        }
        public class Clock
        {
            //timer告诉clock时间，clock通过事件再联系Form组件
            // 定义闹钟的两个事件：Tick 和 Alarm
            public event EventHandler<Lefttime> tick;
            public event EventHandler alarm;
            public event EventHandler init;
            private System.Timers.Timer timer;
            private int interval = 1000;
            private int alarmtime = 10;
            private int lefttime = 10;
            public void start(int alarmtime)
            {
                this.alarmtime = alarmtime;
                this.lefttime = alarmtime;
                tick?.Invoke(this, new Lefttime(lefttime));
                timer.Start();
            }
            public Clock()
            {
                timer = new System.Timers.Timer(interval);
                timer.Elapsed += OnTick;
            }
            public void stop()
            {
                timer.Stop();
            }
            private void OnTick(object sender, EventArgs e)
            {
                if (lefttime > 0)
                {
                    lefttime--;
                    tick?.Invoke(this, new Lefttime(lefttime));


                    // 如果时间到了，触发 Alarm 事件
                    if (lefttime == 0)
                    {
                        alarm?.Invoke(this, EventArgs.Empty);
                        stop();  // 停止定时器
                    }
                }
            }
        }

    }
}
